//
//  PushJob.swift
//  LocationTrackerAdmin
//
//  Created by mac on 2025/3/6.
//

import Vapor
import Queues
import FluentKit
import NIO
import NIOHTTP1
import NIOHTTP2
import NIOSSL
import JWTKit
struct PushJob: AsyncJob {
    struct Payload: Codable {
        let receivers: [String]
        let eventId: String
        let title: String
        let body: String
        let avatar: String
        let mutableContent: Bool
    }

    func dequeue(_ context: QueueContext, _ payload: Payload) async throws {
        let title = payload.title
        let body = payload.body
        let mutableContent = payload.mutableContent
        let eventId = payload.eventId
        let avatar = payload.avatar
        let receivers = payload.receivers
        context.application.apns.sendBatch(tokens: receivers, title: title, body: body, mutableContent: mutableContent ? 1 : nil, customData: ["eventID": eventId, "avatar": avatar])
    }

    func error(_ context: QueueContext, _ error: Error, _ payload: Payload) -> EventLoopFuture<Void> {
        context.application.logger.error("PrintJob failed: \(error.localizedDescription)")
        return context.eventLoop.makeSucceededFuture(())
    }
}
